<?php
namespace Platform\Domain;

use Platform\Common\Functions;
use Platform\Model\Admin as AdminModel;
use Platform\Model\AdminRole as AdminRoleModel;
use PhalApi\Exception\BadRequestException;

class Admin extends Functions {
    //获取用户列表
    public function userList($where,$select,$order,$page,$perpage) {
        $model = new AdminModel();
        $AdminRoleModel = new AdminRoleModel();
        $data = array();
        //需要判断是不是超级管理员，如果不是需要筛选出自己和自己创建的人的数据
        if( $this->tokenData['role_name'] != '超级管理员' ){
            $where['add_who = ? or id = ?'] = array($this->tokenData['id'],$this->tokenData['id']);
            $where['not role_id'] = array(1);
        }
        $where['delete_flag'] = 'N';
        $count = $model->count($where,'id');
        $list = $model->getList($where,array(),$select,$order,$page,$perpage);
        if( !empty( $list ) ){
            foreach ( $list as $k => $v ){
                //查询用户关联的角色名称
                $info = $AdminRoleModel->get($v['role_id'],'delete_flag,role_name');
                if( empty( $info ) || $info['delete_flag']=='Y' ){
                    throw new BadRequestException('所属角色不存在，请检查！');
                }
                $list[$k]['role_name'] = $info['role_name'];
                //查询当前关联的专场(店铺)
                //判断当前账号是不是自己的，是自己的不允许删除，编辑也只能修改名称
                if( $v['id'] == $this->tokenData['id'] ){
                    $list[$k]['is_self'] = 1;
                }else{
                    $list[$k]['is_self'] = 0;
                }
            }
        }
        $data['items'] = $list;
        $data['total'] = $count;
        return $data;
    }
    //添加用户接口
    public function addUser($insertData){
        $model = new AdminModel();
        //查询当前用户名是否已存在
        $userData = $model->getData(array('name'=>$insertData['name']));
        if( !empty($userData)&&$userData['delete_flag']=='N' ){
            throw new BadRequestException('该用户名称已存在！');
        }
        //设置盐值
        $salt = \PhalApi\Tool::createRandStr(64);
        $insertData['salt'] = $salt;
        //解密用户密码并加密
        $insertData['password'] = $this->decode($insertData['password']);
        $insertData['password'] = $this->encryptPassword($insertData['password'],$salt);
        //获取固定的参数
        $insertData = $this->selfAddFields($insertData);
        $res = $model->insert($insertData);
        if( $res === false ){
            throw new BadRequestException('用户添加错误，请联系管理员！');
        }
        return $res;
    }
    //获取用户详情
    public function userDetail($id,$select){
        $model = new AdminModel();
        $userData = $model->getData(array('id'=>$id),array(),$select);
        if( empty($userData)&&$userData['delete_flag']=='Y' ){
            throw new BadRequestException('用户不存在或已删除！');
        }
        //判断是不是改的自己，除了超级管理员，不允许任何人修改自己的关联关系
        if($id == $this->tokenData['id']){
            $userData['is_self'] = 1;
        }else{
            $userData['is_self'] = 0;
        }
        return $userData;
    }
    //编辑提交用户密码
    public function updatePass($id,$updateData){
        $model = new AdminModel();
        //先看看密码是否要修改
        if( isset($updateData['password']) ){
            //设置盐值
            $salt = \PhalApi\Tool::createRandStr(64);
            $updateData['salt'] = $salt;
            //解密用户密码并加密
            $updateData['password'] = $this->decode($updateData['password']);
            $updateData['password'] = $this->encryptPassword($updateData['password'],$salt);
        }
        //获取固定的参数
        $updateData = $this->selfEditFields($updateData);
        $res = $model->update($id,$updateData);
        if( $res === false ){
            throw new BadRequestException('密码提交错误，请联系管理员！');
        }
        return $res;
    }
    //编辑提交用户
    public function updateUser($id,$updateData){
        $model = new AdminModel();
        //查询当前用户名是否已存在
        $userData = $model->getData(array('name'=>$updateData['name']));
        if( !empty($userData)&&$userData['delete_flag']=='N'&&$userData['id'] != $id ){
            throw new BadRequestException('该用户已存在！');
        }
        //先看看密码是否要修改
        if( isset($updateData['password']) ){
            //设置盐值
            $salt = \PhalApi\Tool::createRandStr(64);
            $updateData['salt'] = $salt;
            //解密用户密码并加密
            $updateData['password'] = $this->decode($updateData['password']);
            $updateData['password'] = $this->encryptPassword($updateData['password'],$salt);
        }
        //获取固定的参数
        $updateData = $this->selfEditFields($updateData);
        $res = $model->update($id,$updateData);
        if( $res === false ){
            throw new BadRequestException('用户编辑提交错误，请联系管理员！');
        }
        return $res;
    }
    //删除
    public function deleteUser($id){
        $model = new AdminModel();
        $info = $model->getData(array('id'=>$id));
        if( empty( $info ) ){
            throw new BadRequestException('数据不存在！');
        }
        if( $info['delete_flag'] =='Y' ){
            throw new BadRequestException('数据已被删除！');
        }
        //获取固定的参数
        $updateData = $this->selfDeleteFields(array());
        $res = $model->updateAll(array('id'=>$id),$updateData);
        if( $res === false ){
            throw new BadRequestException('主数据删除有误，请联系管理员！');
        }
        return $res;
    }
}
